package com.example.ymm.util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class SignInCounter {


    public static void main(String[] args) {
        String[] date_str={"2023-08-01","2023-06-01","2024-08-01","2024-08-02","2024-08-03","2024-08-04","2024-08-06","2024-08-15","2024-08-16"};
        String month="2024-08";
        int thisMonthSignInDays = getThisMonthSignInDays(Arrays.asList(date_str), month);
        System.out.println("--连续打卡天数:"+thisMonthSignInDays);
    }

    public static void main2(String[] args) {

    }

    /**
     * 查询这年的连续签到月 数
     * @param date_str 日期格式 天要统一 为2024-02-01，2024-03-01 不能 2024-02-01，2024-03-05
     *        year   月份字符串 2024
     * @return
     */
    public static int getThisYearSignInMonths(List<String> date_str,String year){
        List<LocalDate> dates=new ArrayList<>();
        for(String s:date_str){
            s+="-01";
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(s, formatter);
            dates.add(date);
        }
        String all_month=year+"-01-01";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate this_month_date = LocalDate.parse(all_month, formatter);

        // 当年的第一天
        LocalDate currentYear =this_month_date.withMonth(1);
        int maxConsecutiveDays = getMonthMaxConsecutiveSignInDays(currentYear, dates);
        System.out.println("连续签到月数: " + maxConsecutiveDays);
        return maxConsecutiveDays;
    }

    /**
     * 查询这个月的连续签到天数
     * @param date_str
     *        month   月份字符串 2024-07
     * @return
     */
    public static int getThisMonthSignInDays(List<String> date_str,String month){
        List<LocalDate> dates=new ArrayList<>();
        for(String s:date_str){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate date = LocalDate.parse(s, formatter);
            dates.add(date);
        }
        String all_month=month+"-01";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate this_month_date = LocalDate.parse(all_month, formatter);

        // 当月的第一天
        LocalDate currentMonth =this_month_date.withDayOfMonth(1);
        int maxConsecutiveDays = getMaxConsecutiveSignInDays(currentMonth, dates);
        System.out.println("连续签到天数: " + maxConsecutiveDays);
        return maxConsecutiveDays;
    }


    /**
     * 月 连续打卡计数器
     * @param currentMonth
     * @param signInDates
     * @return
     */
    private static int getMonthMaxConsecutiveSignInDays(LocalDate currentMonth, List<LocalDate> signInDates) {
        Map<LocalDate, Integer> signInMap = new HashMap<>();
        int maxConsecutiveDays = 0;
        for (LocalDate date : signInDates) {
            if (date.getYear() == currentMonth.getYear() ) {
                signInMap.put(date, 1);
            }
        }

        for (LocalDate date = currentMonth; !date.isAfter(currentMonth.withDayOfYear(currentMonth.lengthOfYear())); date = date.plusMonths(1)) {
            if (signInMap.containsKey(date)) {
                int count = 1;
                LocalDate nextDate = date.plusMonths(1);
                while (signInMap.containsKey(nextDate)) {
                    count++;
                    nextDate = nextDate.plusMonths(1);
                }
                maxConsecutiveDays = Math.max(maxConsecutiveDays, count);
            }
        }

        return maxConsecutiveDays;
    }

    /**
     * 天连续打卡计数器
     * @param currentMonth
     * @param signInDates
     * @return
     */
    private static int getMaxConsecutiveSignInDays(LocalDate currentMonth, List<LocalDate> signInDates) {
        Map<LocalDate, Integer> signInMap = new HashMap<>();
        int maxConsecutiveDays = 0;
        for (LocalDate date : signInDates) {
            if (date.getYear() == currentMonth.getYear() && date.getMonth() == currentMonth.getMonth()) {
                signInMap.put(date, 1);
            }
        }

        for (LocalDate date = currentMonth; !date.isAfter(currentMonth.withDayOfMonth(currentMonth.lengthOfMonth())); date = date.plusDays(1)) {
            if (signInMap.containsKey(date)) {
                int count = 1;
                LocalDate nextDate = date.plusDays(1);
                while (signInMap.containsKey(nextDate)) {
                    count++;
                    nextDate = nextDate.plusDays(1);
                    if(!signInMap.containsKey(nextDate)){
                        count=1;
                    }
                }
                maxConsecutiveDays = Math.max(maxConsecutiveDays, count);
            }
        }

        return maxConsecutiveDays;
    }
}
